package com.woniu.service.impl;

import com.woniu.entity.Warehouse;
import com.woniu.mapper.WarehouseMapper;
import com.woniu.service.WarehouseService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {
    @Resource
    private WarehouseMapper warehouseMapper;
    @Override
    public ResponseUtil addWarehouse(Warehouse warehouse) {
        warehouse.setStatus(1); // 默认为启用状态
        warehouse.setUpdateTime(new Date());
        int result = warehouseMapper.add(warehouse);
        return result > 0 ? ResponseUtil.get(ResponseEnum.SUCCESS, null) :
                ResponseUtil.get(ResponseEnum.ERROR, null);
    }

    @Override
    public ResponseUtil deleteWarehouse(Integer warehouseId) {
        // 检查是否被引用
        int count = warehouseMapper.checkReference(warehouseId);
        if (count > 0) {
            return ResponseUtil.get(ResponseEnum.REFERENCED, null);
        }

        int result = warehouseMapper.delete(warehouseId);
        return result > 0 ? ResponseUtil.get(ResponseEnum.SUCCESS, null) :
                ResponseUtil.get(ResponseEnum.ERROR, null);
    }

    @Override
    public ResponseUtil updateWarehouse(Warehouse warehouse) {
        warehouse.setUpdateTime(new Date());
        int result = warehouseMapper.update(warehouse);
        return result > 0 ? ResponseUtil.get(ResponseEnum.SUCCESS, null) :
                ResponseUtil.get(ResponseEnum.ERROR, null);
    }

    @Override
    public ResponseUtil getWarehouseById(Integer warehouseId) {
        Warehouse warehouse = warehouseMapper.findById(warehouseId);
        return warehouse != null ? ResponseUtil.get(ResponseEnum.SUCCESS, warehouse) :
                ResponseUtil.get(ResponseEnum.DATA_NOT_FOUND, null);
    }

    @Override
    public ResponseUtil getAllWarehouses() {
        List<Warehouse> warehouses = warehouseMapper.findAll();
        return ResponseUtil.get(ResponseEnum.SUCCESS, warehouses);
    }

    @Override
    public ResponseUtil searchWarehouses(String name, String address) {
        List<Warehouse> warehouses = warehouseMapper.findByCondition(name, address);
        return ResponseUtil.get(ResponseEnum.SUCCESS, warehouses);
    }

    @Override
    public ResponseUtil disableWarehouse(Warehouse warehouse) {
        // 检查是否被引用
        int count = warehouseMapper.checkReference(warehouse.getWarehouseId());
        if (count > 0) {
            return ResponseUtil.get(ResponseEnum.REFERENCED, null);
        }
        Warehouse warehouse1 = warehouseMapper.findById(warehouse.getWarehouseId());
        if (warehouse1.getStatus()==0) {
            warehouse1.setStatus(1);
        }else {
            warehouse1.setStatus(0);
        }
        int result = warehouseMapper.disableWarehouse(warehouse1);
        return result > 0 ? ResponseUtil.get(ResponseEnum.SUCCESS, null) :
                ResponseUtil.get(ResponseEnum.ERROR, null);
    }
}